package com.bto.solar.work.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bto.solar.work.common.BizException;
import com.bto.solar.work.common.R;
import com.bto.solar.work.constant.BizCode;
import com.bto.solar.work.dao.IAreaService;
import com.bto.solar.work.dao.ILeaseLessorService;
import com.bto.solar.work.dto.LeaseLessorRes;
import com.bto.solar.work.entity.LeaseLessor;
import com.bto.solar.work.utils.BeanUtils;
import com.bto.solar.work.utils.DepartmentUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author shiyi
 */
@Service
public class BtoLeaseLessorService {

    @Autowired
    private ILeaseLessorService iLeaseLessorService;

    @Autowired
    private IAreaService iAreaService;

    public PageInfo<LeaseLessorRes> getLeaseLessorList(Integer page , Integer limit){
        LambdaQueryWrapper<LeaseLessor> leaseLessorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        leaseLessorLambdaQueryWrapper.orderByDesc(LeaseLessor::getId);
        PageHelper.startPage(page,limit);
        List<LeaseLessor> leaseLessors = iLeaseLessorService.list(leaseLessorLambdaQueryWrapper);
        PageInfo<LeaseLessor> pageInfo = PageInfo.of(leaseLessors);
        ArrayList<LeaseLessorRes> lessorRes = new ArrayList<>();
        leaseLessors.forEach(leaseLessor -> {
            LeaseLessorRes leaseLessorRes = DepartmentUtils.copyLeaseLessor(leaseLessor);
            leaseLessorRes.setArea(iAreaService.getById(leaseLessor.getAreaId()));
            lessorRes.add(leaseLessorRes);
        });
        PageInfo<LeaseLessorRes> info = new PageInfo<>();
        BeanUtils.copyPropertiesIgnoreNullValue(pageInfo , info);
        info.setList(lessorRes);
        return info;
    }


    public R<String> addLeaseLessor(LeaseLessor leaseLessor){
        if (leaseLessor == null){
            throw new BizException(BizCode.PARAM_ERROR);
        }
        LambdaQueryWrapper<LeaseLessor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LeaseLessor::getCode , leaseLessor.getCode());
        LeaseLessor one = iLeaseLessorService.getOne(wrapper);
        if (one != null){
            throw new BizException(BizCode.LEASE_REDO_ERROR);
        }
        Date date = new Date(System.currentTimeMillis());
        long time = date.getTime();
        leaseLessor.setUpdateTime(new Timestamp(time));
        leaseLessor.setCreateTime(new Timestamp(time));
        boolean save = iLeaseLessorService.save(leaseLessor);
        if (save){
            return R.ok("新增成功");
        }else {
            return R.fail("新增异常");
        }
    }

    public R<String> updateLeaseLessor(LeaseLessor leaseLessor , Integer id){
        if (leaseLessor == null){
            throw new BizException(BizCode.PARAM_ERROR);
        }
        LambdaQueryWrapper<LeaseLessor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LeaseLessor::getId , id);
        LeaseLessor leaseLessorOne = iLeaseLessorService.getOne(wrapper);
        if (leaseLessorOne == null){
            throw new BizException(BizCode.LEASE_ABSENT_ERROR);
        }
        Date date = new Date(System.currentTimeMillis());
        long time = date.getTime();
        leaseLessor.setUpdateTime(new Timestamp(time));
        boolean update = iLeaseLessorService.update(leaseLessor, wrapper);
        if (update){
            return R.ok("修改成功");
        }else {
            return R.fail("修改异常");
        }
    }

    public R<String> deleteLeaseLessor(Integer id){
        LambdaQueryWrapper<LeaseLessor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LeaseLessor::getId , id );
        if (iLeaseLessorService.getOne(wrapper) == null){
            throw new BizException(BizCode.LEASE_ABSENT_ERROR);
        }
        boolean remove = iLeaseLessorService.remove(wrapper);
        if (remove){
            return R.ok("删除成功");
        }else {
            return R.fail("删除异常");
        }
    }

    public List<LeaseLessor> getCompanyByName(String companyName) {
        return iLeaseLessorService.lambdaQuery().like(LeaseLessor::getCompanyName, companyName).list();
    }
}
